<template>
	<div>
		<ProTable ref="proTable" title="角色权限" :columns="columns" :request-api="getTableList" :init-param="initParam">
			<template #tableHeader>
				<el-button class="mt20" type="primary" v-auth="'system:role:create:invoke'" @click="toUpdate()"> 新增角色 </el-button>
			</template>
			<!-- Expand -->
			<template #expand="scope">
				{{ scope.row }}
			</template>

			<!-- 表格操作 -->
			<template #operation="{ row }">
				<el-button type="primary" link v-auth-all="['system:role:details:invoke', 'system:role:edit:invoke']" @click="toUpdate(row)">
					编辑
				</el-button>
				<el-divider direction="vertical" v-auth-all="['system:role:details:invoke', 'system:role:edit:invoke']" />
				<el-button type="primary" link v-auth="'system:role:delete:invoke'" @click="onDelete(row)"> 移除 </el-button>
			</template>
		</ProTable>
	</div>
</template>

<script setup lang="tsx" name="storeList">
import { onMounted, ref, reactive } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { NextLoading } from '/@/utils/loading';
import ProTable from '../../../../components/ProTable/index.vue';
import { ProTableInstance, ColumnProps } from '../../../../components/ProTable/interface';
import { fetchRoleList, fetchDelRole } from '/@/api/system';
import { useRouter } from 'vue-router';

const router = useRouter();

// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref<ProTableInstance>();

// 如果表格需要初始化请求参数，直接定义传给 ProTable(之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
const initParam = reactive({});

// 如果你想在请求之前对当前请求参数做一些操作，可以自定义如下函数：params 为当前所有的请求参数（包括分页），最后返回请求列表接口
// 默认不做操作就直接在 ProTable 组件上绑定	:requestApi="getTableList"
const getTableList = (params: any) => {
	return fetchRoleList(params);
};

// 表格配置项
const columns: ColumnProps<any>[] = [
	{
		prop: 'roleName',
		label: '角色名称',
		minWidth: 100,
	},
	{
		prop: 'permissions',
		label: '权限明细',
		minWidth: 100,
	},
	{ prop: 'operation', label: '操作', fixed: 'right', width: 120, align: 'left' },
];

const toUpdate = (row?: any) => {
	router.push(row ? `/system/role/edit/${row.id}` : `/system/role/add`);
};

const onDelete = async (row: any) => {
	ElMessageBox.confirm('确认删除该角色?', '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(async () => {
		const res = await fetchDelRole({ id: row.id });
		if (res.code == 200) {
			ElMessage.success('移除成功');
			proTable.value?.getTableList();
		}
	});
};

// 页面加载时
onMounted(() => {
	NextLoading.done();
});
</script>

<style lang="scss">
.demo-tabs {
	.el-tabs__header {
		margin-bottom: 0;
	}
}
</style>
